import 'dart:developer';

import 'package:get/get.dart';
import 'package:sports_new/controller/match/detail/zq_match_detail_logic.dart';
import 'package:sports_new/entity/match/zq_lineup_entity.dart';
import 'package:sports_new/entity/match/zq_match_event_entity.dart';
import 'package:sports_new/server/api/soccer_api.dart';
import 'package:sports_new/utils/utils.dart';

class ZqMatchLineupLogic extends GetxController {
  final detail = Get.find<ZqMatchDetailLogic>(tag: '${Get.arguments}');

  ZqLineupEntity? data;
  List<ZqLineupEntity?>? nowData = [null, null];
  int _select = 0;
  List<Map<String, List<String>>?> nowEvents = [null, null];
  Map<String, List<String>>? playerEvents;
  bool isLoading = true;
  int get select => _select;

  @override
  void onInit() async {
    super.onInit();
    await getLineup();
  }

  Future getLineup() async {
    nowData = await SoccerApi.getZqMatchLineup(detail.matchId);
    await getEvents(0);
    await getEvents(1);
    if (nowData?[1]?.homeLineup.hasValue == true) {
      _select = 1;
      data = nowData?[1];
      playerEvents = nowEvents[1];
    } else if (nowData?[0]?.homeLineup?.length.hasValue == true) {
      _select = 0;
      data = nowData?[0];
      playerEvents = nowEvents[0];
    } else {
      _select = 1;
      data = nowData?[1];
      playerEvents = nowEvents[1];
    }
    isLoading = false;
    update();
  }

  Future getEvents(int index) async {
    final events = await Utils.tryOrNullf(() async {
      var response;
      List l;
      if (index == 1) {
        response = await SoccerApi.getZqMatchEvent(detail.matchId, index);
        l = (response?.data['d'] as List)
            .map((e) => ZqMatchEventEntity.fromJson(e))
            .toList();
      } else {
        List v;
        v = nowData?[0]?.guestEvents ?? [];
        v.addAll(nowData?[0]?.homeEvents ?? []);
        l = v;
        Future.delayed(const Duration(milliseconds: 100));
      }
      var map = <String, List<String>>{};
      for (ZqMatchEventEntity element in l) {
        if (!map.containsKey(element.playerId) && element.playerId.hasValue) {
          map[element.playerId!] = ["", "", "", "", ""];
          if (element.playerId2.hasValue && !map.containsKey(element.playerId2)) {
            map[element.playerId2!] = ["", "", "", "", ""];
          }
        }
        switch (element.kind) {
          case 1:
            map[element.playerId]![3] = '进球';
            break;
          case 11:
            try {
              map[element.playerId]![0] = '上场';
              map[element.playerId]![1] = element.eventTime.toString();
              if (element.playerId2.hasValue) {
                map[element.playerId2]![0] = '下场';
                map[element.playerId2]![1] = element.eventTime.toString();
              }
            }catch(e){
              log(e.toString());
            }
            break;
          case 3:
            map[element.playerId]![2] = '黄牌';
            break;
          case 2:
            map[element.playerId]![2] = '红牌';
            break;
          case 9:
            map[element.playerId]![2] = '两黄';
            break;
          case 7:
            map[element.playerId]![3] = '点球';
            break;
          case 8:
            map[element.playerId]![3] = '乌龙';
            break;
          case 49:
            map[element.playerId]![4] = '角球';
            break;
        }
      }
      return map;
    });
    nowEvents[index] = events;
    update();
  }

  String toArray(bool isHome) {
    String? array = isHome?data?.homeArray:data?.guestArray;
    if (array.hasValue == true) {
      return "阵型 ${array!.split("").join("-")}";
    } else {
      return "阵型暂无";
    }
  }

  String ageAndWorth(bool isHome) {
    int? age = isHome?data?.homeAverageAge:data?.guestAverageAge;
    String? worth = isHome?data?.homeSumWorth:data?.guestSumWorth;
    return [
      if(age.hasValue == true)
        "平均$age岁",
      if(worth.hasValue)
        worth
    ].join(" ");
  }

  void lineupChoice(index) {
    _select = index;
    data = nowData?[index];
    playerEvents = nowEvents[index];
    update();
  }
}
